package cn.liyicun.cp.cpweb.controller.admin;

import cn.liyicun.cp.cpweb.base.Result;
import cn.liyicun.cp.cpweb.entity.AdminInfo;
import cn.liyicun.cp.cpweb.entity.AdminInfoExample;
import cn.liyicun.cp.cpweb.entity.LoginInfo;
import cn.liyicun.cp.cpweb.mapper.AdminInfoMapper;
import cn.liyicun.cp.cpweb.service.SettingInfoService;
import cn.liyicun.cp.cpweb.util.CPConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = {CPConstant.ROOT_PHONE_ADMIN})
public class IndexAdminController extends BaseAdminController {
    @Autowired
    AdminInfoMapper adminInfoMapper;
    @Value("${cp.default.test}")
    boolean test;
    @Autowired
    private SettingInfoService settingInfoService;

    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request, Map<String, Object> map) {

        LoginInfo info = getLoginInfo(request, 2);
        map.put("title", "CP后台管理");
        map.put("now", new Date());
        map.put("info", info);
        // 跳转到 templates 目录下的 uploadimg.html
        return "admin/index";
    }


    @RequestMapping(value = "/clear")
    public void clear(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) throws IOException {
        LoginInfo info = getLoginInfo(request, 2);
        settingInfoService.clear();
        response.sendRedirect("/admin/index");
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request, Map<String, Object> map) {
        map.put("title", "CP后台管理");
        map.put("now", new Date());


        // 跳转到 templates 目录下的 uploadimg.html
        return "admin/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> loginPost(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "email", required = true) String email,
                               @RequestParam(value = "passwd", required = true) String passwd,
                               @RequestParam(value = "saveAccount", required = false) String saveAccount
            ,
                               @RequestParam(value = "captcha", required = true) String captcha
    ) {

        if (!test) {
            Object data = getAttribute(request, CPConstant.captcha);
            compare(data == null, "验证码失效!");
            removeAttribute(request, CPConstant.captcha);
            compare(!data.toString().equalsIgnoreCase(captcha), "验证码错误!");
        }

        AdminInfoExample example = new AdminInfoExample();
        example.createCriteria().andEmailEqualTo(email);
        List<AdminInfo> list = adminInfoMapper.selectByExample(example);
        compare(list.isEmpty(), "账号或者密码错误!");
        compare(list.size() != 1, "账号或者密码错误!");
        AdminInfo admin = list.get(0);
        compare(admin.getStatus() != 1, "账号禁用!");
        compare(!passwd.equals(admin.getPasswd()), "账号或者密码错误!");
        LoginInfo info = new LoginInfo();
        info.setUid(admin.getId());
        info.setUserName(admin.getEmail());
        setLoginInfo(request, info);
        if ("1".equals(saveAccount)) {
            response.addCookie(new Cookie("email", email));
        } else {
            Cookie cookie = new Cookie("email", null);
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
        return Result.success("登陆成功!");
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void logout(HttpServletResponse response, Map<String, Object> map, HttpServletRequest request) throws IOException {
        logout(request);
        response.sendRedirect("/admin/index");
    }


}
